package com.zmxstudy.gzu.controller;


import com.zmxstudy.gzu.domain.TbSku;
import com.zmxstudy.gzu.mapper.TbSkuMapper;
import com.zmxstudy.gzu.utils.ProductCodeMsg;
import com.zmxstudy.gzu.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

@Tag(name = "库存管理")
@RestController
@RequestMapping("/product-service/inventorys")
@SuppressWarnings("rawtypes")
public class InventoryController {
    @Resource
    TbSkuMapper tbSkuMapper;

    @Operation(summary = "库存减加，需传递两个参数，id<商品ID>, sum<下单数量>")
    @GetMapping("/reduce")
    Result reduce(@Param(value = "id") Long id, @Param(value = "sum") int sum) {
        TbSku sku = tbSkuMapper.selectById(id);

        if (sum > sku.getNum()) {
            return Result.error(ProductCodeMsg.inventory_error);
        }
        sum = sku.getNum() - sum;
        sku.setNum(sum);

        tbSkuMapper.updateById(sku);
        return Result.success();

    }


    @Operation(summary = "库存增加，需传递两个参数，id<商品ID>, sum<下单数量>")
    @GetMapping("/add")
    Result add(@Param(value = "id") Long id, @Param(value = "sum") int sum) {
        TbSku sku = tbSkuMapper.selectById(id);

        if (sum < 0) {
            return Result.error(ProductCodeMsg.inventory_add_error);
        }
        sum = sku.getNum() + sum;
        sku.setNum(sum);

        tbSkuMapper.updateById(sku);
        return Result.success();

    }


}
